package selenium.commons;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import selenium.linkedin.entity.Applicant;
import selenium.linkedin.entity.User;

@Component
public class PageNavigator {

	@Autowired
	SeleniumUtils seleniumUtils;

	public void successfullLogIn(User user) {
		seleniumUtils.goToPage("http://www.linkedin.com/");
		LoggingPage loggingPage = new LoggingPage();
		seleniumUtils.waitForObjectAndpopulateIt(
				loggingPage.getUserNameFieldXpath(), user.getMail());
		seleniumUtils.waitForObjectAndpopulateIt(
				loggingPage.getPasswordFieldXpath(), user.getPassword());
		seleniumUtils.pressEnterOn(loggingPage.getPasswordFieldXpath());
	}

	public void populateSearchPage(Applicant applicant) {
		SearchPage searchPage = new SearchPage();

		seleniumUtils.waitForObjectAndpopulateIt(
				searchPage.getCargoFieldXpath(), applicant.getCargo());
		seleniumUtils.waitForObjectAndpopulateIt(
				searchPage.getCompanyFieldXpath(), applicant.getCompany());
		seleniumUtils.waitForObjectAndpopulateIt(
				searchPage.getFirstNameFieldXpath(), applicant.getFirstName());
		seleniumUtils.waitForObjectAndpopulateIt(
				searchPage.getKeyWordFieldXpath(), applicant.getKeyWord());
		seleniumUtils.waitForObjectAndpopulateIt(
				searchPage.getLastNameFieldXpath(), applicant.getLastName());
		seleniumUtils
				.waitForObjectAndpopulateIt(
						searchPage.getUniversityFieldXpath(),
						applicant.getUniversity());
		seleniumUtils.waitForObjectAndClickOnIt(searchPage
				.getSearchButtonXpath());

		 Set<String> applicants=getApplicantsResultSet();
		 
		 giveMeApplicants(applicants);

	}
	
	public List<Applicant> giveMeApplicants (Set<String> applicantsUrls)
	{
		 List<Applicant> allTheApplicants=new ArrayList<Applicant>();
		 for (String url : applicantsUrls) {
			seleniumUtils.goToPage(url);
			Applicant applicant=new Applicant();
			setApplicantLinkedinProfile(applicant);
			allTheApplicants.add(applicant);
		}
		return allTheApplicants;
	}
	
	public void setApplicantLinkedinProfile(Applicant applicant)
	{
		applicant.setFullName(getApplicantName());
		applicant.setPublicProfile(getApplicantPublicProfile());
		applicant.setExtract(getExtract());
		applicant.setCourses(getApplicantCourses());
		applicant.setCompanies(getCompanies());
	}

	public Set<String> getApplicantsResultSet() {
		List<String> listOfUrl = seleniumUtils.waitForListAnReturnAtributes(
				"//*[@class='paginator-curr']", "href");
		Set<String> applicantsUrls = new HashSet<String>();
		listOfUrl.add(seleniumUtils.getActualPageLink());

		for (String url : listOfUrl) {
			seleniumUtils.goToPage(url);
			applicantsUrls
					.addAll(seleniumUtils
							.waitForListAnReturnAtributes(
									"//*[@class='fn n external-link trk-profile-name']",
									"href"));
		}
		return applicantsUrls;
	}
	
	public String getApplicantPublicProfile()
	{
		
		String publicProfile = seleniumUtils
				.waitForObjectAnReturnTextValue(
						"//*[@class='public-profile']");
		return publicProfile;
	}
	
	
	public String getApplicantName()
	{
		
		String personalInformation = seleniumUtils
				.waitForObjectAnReturnTextValue(
						"//*[@class='full-name']");
		return personalInformation;
	}
	
	public Set<String> getApplicantCourses()
	{
		
		Set<String> courses = seleniumUtils
				.waitForListAnReturnTextResultSet(
						"//*[@class='courses-listing']");
		return courses;
	}
	
	public String getExtract()
	{
		String extract = seleniumUtils
				.waitForObjectAnReturnTextValue(
						"//*[@class='description']");
		return extract;
	}

	public Set<String> getCompanies() {
		Set<String> companies = seleniumUtils
				.waitForListAnReturnTextResultSet(
						"//*[@data-tracking='mcp_profile_sum']");
		return companies;
	}

	public void successfullClose() {
		seleniumUtils.closeDriver();
	}

	public void goToSearchPage() {
		seleniumUtils.waitForObjectAndClickOnIt("//*[@id='search-link']");
	}

}